# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

# note on all fiels must be optional
class Unit(User):
    about = models.TextField(u'О себе', blank=True)

class Squad(models.Model):
    is_active = models.BooleanField(default=True)
    name = models.CharField(u'Имя', max_length=200, null=False)
    leader = models.ForeignKey(User, unique=True, related_name='leader')
    members = models.ManyToManyField(User, null = True, related_name='members')

    def __unicode__(self):
        return self.name

class SquadJoinRequest(models.Model):
    user = models.ForeignKey(User)
    squad = models.ForeignKey(Squad)
    team_accepted = models.BooleanField(default=False)
    user_accepted = models.BooleanField(default=False)

class GameAbstract(models.Model):
    title = models.CharField(u'Название', max_length=200, null=False)
    created_timestamp = models.DateTimeField(auto_now_add = True)
    changed_timestamp = models.DateTimeField(auto_now = True)

    def __unicode__(self):
        return self.title

class Game(GameAbstract):
    GAME_STYLES = (
        (u'linear',  u'Линейная'),
        (u'classic', u'Классическая'),
        (u'flags',   u'Флаги'),
        )
    GAME_LEAGUES = (
        (u'high',  u'высшая'),
        (u'first', u'первая'),
#        (u'', u''),
        )

    slug               = models.SlugField(u'Слаг', editable=False)
    start_timestamp    = models.DateTimeField(u'Старт в')
    end_timestamp      = models.DateTimeField(u'Окончание в', null=True)
    authors            = models.ManyToManyField(User, related_name = 'author')
    credit_to          = models.ForeignKey(Squad, related_name = 'credit', null=True)
    radius             = models.PositiveSmallIntegerField(u'Радиус', null=True)
    style              = models.CharField(u'Тип игры', max_length = 20, choices = GAME_STYLES, default = 'linear')
    league             = models.CharField(u'Лига', max_length = 20, choices = GAME_LEAGUES, default = 'first')
    description        = models.TextField(u'Описание', null=True)
    comments           = models.TextField(u'Комментарии', null=True)
    attributes         = models.TextField(u'Принадлежности', null=True)
    registered_squads  = models.ManyToManyField(Squad, null=True, editable=False, related_name = 'registered_squads')

# Задание
class Task(GameAbstract):
    levelnum = models.PositiveSmallIntegerField(u'Номер уровня', null = False, blank = False)
    required = models.PositiveSmallIntegerField(u'Обязателен к прохождению', default = 1)
    timeout = models.TimeField(u'Таймаут', null = False, blank = False)
    text = models.TextField(u'Текст задания', blank=False)
    comment = models.TextField(u'Комментарий к заданию', null=True, editable = False, default='')
    aftersolve_text = models.TextField(u'Текст после прохождения', null=True, editable=False, default='')
    game = models.ForeignKey(Game, related_name='tasks', editable=False)

class Key(models.Model):
    text = models.CharField(u'Ключ', max_length = 20, null = False)
    task = models.ForeignKey(Task, related_name='keys')

    def __unicode__(self):
        return self.text

# Подсказка
class Hint(GameAbstract):
    time_shift = models.TimeField(u'Выдать через', null = False, blank = False)
    text = models.TextField(u'Текст')
    task = models.ForeignKey(Task)

    class Meta:
        unique_together = (('time_shift', 'task'),)

class Engine(models.Model):
    squad = models.ForeignKey(Squad)
    start_time = models.DateTimeField()
